import sys
import os
 
# 定义跨平台getch函数
def getch():
    import termios
    fd = sys.stdin.fileno()
    try:
        ch = sys.stdin.read(1)
    finally:
        pass

    return ch
 
# 监听示例
print("按任意键，ESC键退出....")
while True:
    key = getch()
    print(f"按键: {key}")
    if key == '\x1b':  # ESC键
        break